package com.greenantmall.view;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.app.Dialog;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.greenantmall.R;
import com.greenantmall.activity.ShoppingCartActivity;
import com.greenantmall.adapter.ChooseDateListAdapter;
import com.greenantmall.adapter.ChooseDateListAdapter.ChooseDateBean;

public class ChooseDateDialog extends Dialog {
	private ShoppingCartActivity mShoppingCartActivity;
	private List<ChooseDateBean> mTodayDateList = new ArrayList<ChooseDateBean>();
	private List<ChooseDateBean> mTomorrowDateList = new ArrayList<ChooseDateBean>();
	
	private View mLLTabToday;
	private View mLLTabTomorrow;
	private TextView mTvTabToday;
	private TextView mTvTabTomorrow;
	private View mTabTodayLine;
	private View mTabTomorrowLine;
	private Button mBtnSure;
	private Button mBtnCancel;
	private ListView mListView;
	private ChooseDateListAdapter mAdapterToday;
	private ChooseDateListAdapter mAdapterTomorrow;
	
	private Calendar mOpenCalendar;
	private Calendar mCloseCalendar;
	
	// 0:浠婂ぉ锛�1锛氭槑澶�
	private int mCurrentSelect = 0;
	private ChooseDateBean mCurrentChooseDateBean;
	
	private android.view.View.OnClickListener mOnClickListener = new android.view.View.OnClickListener() {
		@Override
		public void onClick(View v) {
			if(v.equals(mTvTabToday)) {
				if(mCurrentSelect == 0) {
					return;
				}
				mCurrentSelect = 0;
				
				mTvTabToday.setTextColor(mShoppingCartActivity.getResources().getColor(R.color.green));
				mTabTodayLine.setBackgroundColor(mShoppingCartActivity.getResources().getColor(R.color.green));
				mTvTabTomorrow.setTextColor(mShoppingCartActivity.getResources().getColor(android.R.color.black));
				mTabTomorrowLine.setBackgroundColor(mShoppingCartActivity.getResources().getColor(android.R.color.darker_gray));
				
				mCurrentChooseDateBean.isSelected = false;
				mAdapterTomorrow.notifyDataSetChanged();
				
				mCurrentChooseDateBean = mTodayDateList.get(0);
				mCurrentChooseDateBean.isSelected = true;
				mListView.setAdapter(mAdapterToday);
			}
			else if(v.equals(mTvTabTomorrow)) {
				if(mCurrentSelect == 1) {
					return;
				}
				mCurrentSelect = 1;
				
				mTvTabToday.setTextColor(mShoppingCartActivity.getResources().getColor(android.R.color.black));
				mTabTodayLine.setBackgroundColor(mShoppingCartActivity.getResources().getColor(android.R.color.darker_gray));
				mTvTabTomorrow.setTextColor(mShoppingCartActivity.getResources().getColor(R.color.green));
				mTabTomorrowLine.setBackgroundColor(mShoppingCartActivity.getResources().getColor(R.color.green));
				
				mCurrentChooseDateBean.isSelected = false;
				mAdapterToday.notifyDataSetChanged();
				
				mCurrentChooseDateBean = mTomorrowDateList.get(0);
				mCurrentChooseDateBean.isSelected = true;
				mListView.setAdapter(mAdapterTomorrow);
			}
			else if(v.equals(mBtnSure)) {
				mShoppingCartActivity.goToOrderCreateActivity(mCurrentChooseDateBean.calendar.getTime());
				dismiss();
			}
			else if(v.equals(mBtnCancel)) {
				dismiss();
			}
		}
	};
	
	private OnItemClickListener mOnItemClickListener = new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			if(mCurrentSelect == 0) {
				for(ChooseDateBean bean : mTodayDateList) {
					bean.isSelected = false;
				}
				mCurrentChooseDateBean = mTodayDateList.get(arg2);
				mCurrentChooseDateBean.isSelected = true;
				mAdapterToday.notifyDataSetChanged();
			}
			else if(mCurrentSelect == 1) {
				for(ChooseDateBean bean : mTomorrowDateList) {
					bean.isSelected = false;
				}
				mCurrentChooseDateBean = mTomorrowDateList.get(arg2);
				mCurrentChooseDateBean.isSelected = true;
				mAdapterTomorrow.notifyDataSetChanged();
			}
		}
	};
	
	public ChooseDateDialog(ShoppingCartActivity shoppingCartActivity, Calendar openCalendar, Calendar closeCalendar) {
		super(shoppingCartActivity, R.style.choose_date_dialog_style);
		setContentView(R.layout.choose_date_dialog);
		mShoppingCartActivity = shoppingCartActivity;
		
		mLLTabToday = findViewById(R.id.choose_date_dialog_tab_today_ll);
		mLLTabTomorrow = findViewById(R.id.choose_date_dialog_tab_tomorrow_ll);
		mTvTabToday = (TextView)findViewById(R.id.choose_date_dialog_tab_today);
		mTvTabTomorrow = (TextView)findViewById(R.id.choose_date_dialog_tab_tomorror);
		mTabTodayLine = findViewById(R.id.choose_date_dialog_tab_today_line);
		mTabTomorrowLine = findViewById(R.id.choose_date_dialog_tab_tomorror_line);
		mBtnSure = (Button)findViewById(R.id.choose_date_dialog_bottom_btn_sure);
		mBtnCancel = (Button)findViewById(R.id.choose_date_dialog_bottom_btn_cancel);
		mListView = (ListView)findViewById(R.id.choose_date_date_list);
		
		mBtnSure.setOnClickListener(mOnClickListener);
		mBtnCancel.setOnClickListener(mOnClickListener);
		mTvTabToday.setOnClickListener(mOnClickListener);
		mTvTabTomorrow.setOnClickListener(mOnClickListener);
		mListView.setOnItemClickListener(mOnItemClickListener);
		
		mOpenCalendar = openCalendar;
		mCloseCalendar = closeCalendar;
		
		this.createListDate();
		if(mTodayDateList.size() == 0) {
			mLLTabToday.setVisibility(View.GONE);
			mLLTabTomorrow.setVisibility(View.VISIBLE);
			mTvTabToday.setTextColor(mShoppingCartActivity.getResources().getColor(android.R.color.black));
			mTabTodayLine.setBackgroundColor(mShoppingCartActivity.getResources().getColor(android.R.color.darker_gray));
			mTvTabTomorrow.setTextColor(mShoppingCartActivity.getResources().getColor(R.color.green));
			mTabTomorrowLine.setBackgroundColor(mShoppingCartActivity.getResources().getColor(R.color.green));
			mCurrentSelect = 1;
			
			mCurrentChooseDateBean = mTomorrowDateList.get(0);
			mCurrentChooseDateBean.isSelected = true;
			mAdapterToday = new ChooseDateListAdapter(mShoppingCartActivity, mTodayDateList);
			mAdapterTomorrow = new ChooseDateListAdapter(mShoppingCartActivity, mTomorrowDateList);
			mListView.setAdapter(mAdapterTomorrow);
		}
		else {
			mLLTabToday.setVisibility(View.VISIBLE);
			mLLTabTomorrow.setVisibility(View.VISIBLE);
			mTvTabToday.setTextColor(mShoppingCartActivity.getResources().getColor(R.color.green));
			mTabTodayLine.setBackgroundColor(mShoppingCartActivity.getResources().getColor(R.color.green));
			mTvTabTomorrow.setTextColor(mShoppingCartActivity.getResources().getColor(android.R.color.black));
			mTabTomorrowLine.setBackgroundColor(mShoppingCartActivity.getResources().getColor(android.R.color.darker_gray));
			mCurrentSelect = 0;
			
			mCurrentChooseDateBean = mTodayDateList.get(0);
			mCurrentChooseDateBean.isSelected = true;
			mAdapterToday = new ChooseDateListAdapter(mShoppingCartActivity, mTodayDateList);
			mAdapterTomorrow = new ChooseDateListAdapter(mShoppingCartActivity, mTomorrowDateList);
			mListView.setAdapter(mAdapterToday);
		}
	}

	private void createListDate() {
		int openHour = mOpenCalendar.get(Calendar.HOUR_OF_DAY);
		int openMinute = mOpenCalendar.get(Calendar.MINUTE);
		int closeHour = mCloseCalendar.get(Calendar.HOUR_OF_DAY);
		int closeMinute = mCloseCalendar.get(Calendar.MINUTE);
		
		// tomorrow date list
		for(int i=0; i<24; i++) {
			Calendar calendarTomorrow = Calendar.getInstance();
			calendarTomorrow.add(Calendar.DAY_OF_MONTH, 1);
			calendarTomorrow.set(Calendar.HOUR_OF_DAY, i);
			calendarTomorrow.set(Calendar.MINUTE, 0);
			ChooseDateBean bean = new ChooseDateBean();
			bean.isSelected = false;
			bean.calendar = calendarTomorrow;
			mTomorrowDateList.add(bean);
			
			calendarTomorrow = Calendar.getInstance();
			calendarTomorrow.add(Calendar.DAY_OF_MONTH, 1);
			calendarTomorrow.set(Calendar.HOUR_OF_DAY, i);
			calendarTomorrow.set(Calendar.MINUTE, 30);
			bean = new ChooseDateBean();
			bean.isSelected = false;
			bean.calendar = calendarTomorrow;
			mTomorrowDateList.add(bean);
		}
		
		List<ChooseDateBean> tempList = new ArrayList<ChooseDateBean>();
		for(ChooseDateBean bean : mTomorrowDateList) {
			int beanHour = bean.calendar.get(Calendar.HOUR_OF_DAY);
			int beanMinute = bean.calendar.get(Calendar.MINUTE);
			if((beanHour < openHour || (beanHour==openHour && beanMinute <= openMinute))
					|| (beanHour > closeHour || (beanHour==closeHour && beanMinute >= closeMinute))) {
				tempList.add(bean);
			}
		}
		mTomorrowDateList.removeAll(tempList);
		
		Calendar calendarTomorrow = Calendar.getInstance();
		calendarTomorrow.add(Calendar.DAY_OF_MONTH, 1);
		calendarTomorrow.set(Calendar.HOUR_OF_DAY, openHour);
		calendarTomorrow.set(Calendar.MINUTE, openMinute);
		ChooseDateBean tempBean = new ChooseDateBean();
		tempBean.isSelected = false;
		tempBean.calendar = calendarTomorrow;
		mTomorrowDateList.add(0, tempBean);
		
		calendarTomorrow = Calendar.getInstance();
		calendarTomorrow.add(Calendar.DAY_OF_MONTH, 1);
		calendarTomorrow.set(Calendar.HOUR_OF_DAY, closeHour);
		calendarTomorrow.set(Calendar.MINUTE, closeMinute);
		tempBean = new ChooseDateBean();
		tempBean.isSelected = false;
		tempBean.calendar = calendarTomorrow;
		mTomorrowDateList.add(tempBean);
		
		// today date list
		for(int i=0; i<24; i++) {
			Calendar calendarToday = Calendar.getInstance();
			calendarToday.set(Calendar.HOUR_OF_DAY, i);
			calendarToday.set(Calendar.MINUTE, 0);
			ChooseDateBean bean = new ChooseDateBean();
			bean.isSelected = false;
			bean.calendar = calendarToday;
			mTodayDateList.add(bean);
			
			calendarToday = Calendar.getInstance();
			calendarToday.set(Calendar.HOUR_OF_DAY, i);
			calendarToday.set(Calendar.MINUTE, 30);
			bean = new ChooseDateBean();
			bean.isSelected = false;
			bean.calendar = calendarToday;
			mTodayDateList.add(bean);
		}
		
		int startHour = openHour;
		int startMinute = openMinute;
		int endHour = closeHour;
		int endMinute = closeMinute;
		
		Calendar calendar = Calendar.getInstance();
		int nowHour = calendar.get(Calendar.HOUR_OF_DAY);
		int nowMinute = calendar.get(Calendar.MINUTE);
		
		if(startHour < nowHour || (startHour == nowHour && startMinute < nowMinute)) {
			startHour = nowHour;
			startMinute = nowMinute;
		}
		
		if(endHour > startHour || (endHour == startHour && endMinute > startMinute)) {
			tempList.clear();
			for(ChooseDateBean bean : mTodayDateList) {
				int beanHour = bean.calendar.get(Calendar.HOUR_OF_DAY);
				int beanMinute = bean.calendar.get(Calendar.MINUTE);
				if((beanHour < startHour || (beanHour==startHour && beanMinute <= startMinute))
						|| (beanHour > endHour || (beanHour==endHour && beanMinute >= endMinute))) {
					tempList.add(bean);
				}
			}
			mTodayDateList.removeAll(tempList);
			
			Calendar calendarToday = Calendar.getInstance();
			calendarToday.set(Calendar.HOUR_OF_DAY, startHour);
			calendarToday.set(Calendar.MINUTE, startMinute);
			tempBean = new ChooseDateBean();
			tempBean.isSelected = false;
			tempBean.calendar = calendarToday;
			mTodayDateList.add(0, tempBean);
			
			calendarToday = Calendar.getInstance();
			calendarToday.set(Calendar.HOUR_OF_DAY, endHour);
			calendarToday.set(Calendar.MINUTE, endMinute);
			tempBean = new ChooseDateBean();
			tempBean.isSelected = false;
			tempBean.calendar = calendarToday;
			mTodayDateList.add(tempBean);
		}
		else {
			mTodayDateList.clear();
		}
	}
}















